/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.ProcurementSemenCtlr;
import org.gov.vis.data.controller.ProcurementSemenCtlrImpl;
import org.gov.vis.data.dto.ProcurementSemenDto;
import org.gov.vis.data.gui.EditProcurementSemenDialog;
import org.gov.vis.data.gui.ViewProcurementSemenDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewProcurementSemenListener implements ActionListener,BaseListener {
	
	private ProcurementSemenCtlr procurementSemenCtlr = new ProcurementSemenCtlrImpl();
	private ViewProcurementSemenDialog viewProcurementSemenDialog;
	private ProcurementSemenDto procurementSemenDto;
	final private ResourceBundle rb = Util.getResources();
	private EditProcurementSemenDialog editProcurementSemenDialog;

	public ViewProcurementSemenListener(ViewProcurementSemenDialog viewProcurementSemenDialog) {
		// TODO Auto-generated constructor stub
		this.viewProcurementSemenDialog = viewProcurementSemenDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewProcurementSemenDialog _viewProcurementSemenDialog =(ViewProcurementSemenDialog)_guiObject;
		procurementSemenDto = new ProcurementSemenDto();
		
		String code = _viewProcurementSemenDialog.getCodeTxtBx().getText();
		if(code!=null){
		 procurementSemenDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		final ProcurementSemenDto _procurementSemenDto = (ProcurementSemenDto)_dtoObject;
		if(editProcurementSemenDialog != null){
			editProcurementSemenDialog.getTxtFldCode().setText(_procurementSemenDto.getCode());
			editProcurementSemenDialog.getTxtFldBreed().setText(_procurementSemenDto.getBreed());
			editProcurementSemenDialog.getTxtFldBullNo().setText(_procurementSemenDto.getBullNo());
			editProcurementSemenDialog.getTxtFldDiscord().setText(_procurementSemenDto.getDiscord());
			editProcurementSemenDialog.getDtChoDateOfReceipt().setDate(_procurementSemenDto.getDoRcpt());
			editProcurementSemenDialog.getTxtFldFromStation().setText(_procurementSemenDto.getFrmStation());
			editProcurementSemenDialog.getTxtFldNoOfStraws().setText(_procurementSemenDto.getNoStraws());
			editProcurementSemenDialog.setHidnObject(_procurementSemenDto.getPrSemenId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewProcurementSemenDialog);
			try {
				
			List<ProcurementSemenDto> procurementSemenLst = procurementSemenCtlr.searchPrSemens(procurementSemenDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Date of Receipt");
			colLst.add("Breed");
			colLst.add("Bul No");
			colLst.add("From Station");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(procurementSemenLst != null && !procurementSemenLst.isEmpty()){
				for(ProcurementSemenDto dto:procurementSemenLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getDoRcpt());
					lst.add(dto.getBreed());
					lst.add(dto.getBullNo());
					lst.add(dto.getFrmStation());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewProcurementSemenDialog.getPnlProcurementSemenResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			ProcurementSemenDto procurementSemenDto = (ProcurementSemenDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+procurementSemenDto.getCode());
			editProcurementSemenDialog = new EditProcurementSemenDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_prmt_semen_title"),
                    false);
			setDtoDetailsToGui(procurementSemenDto);
			viewProcurementSemenDialog.dispose();

		}
		
	}


}
